package com.bdqn.hospital.service.impl;

import com.bdqn.hospital.mapper.DrugMapper;
import com.bdqn.hospital.mapper.PrescriptionDetailMapper;
import com.bdqn.hospital.mapper.PrescriptionMapper;
import com.bdqn.hospital.pojo.Drug;
import com.bdqn.hospital.pojo.Prescription;
import com.bdqn.hospital.pojo.PrescriptionDetail;
import com.bdqn.hospital.service.PrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version : 1.0
 * @Author :guogd
 * @createTime: 2023/03/01 15:05
 * @Description: com.bdqn.hospital.service.impl
 */
@Service
public class PrescriptionServiceImpl implements PrescriptionService {

    @Autowired
    private PrescriptionMapper prescriptionMapper;
    @Autowired
    private PrescriptionDetailMapper prescriptionDetailMapper;
    @Autowired
    private DrugMapper drugMapper;

    @Transactional
    @Override
    public Prescription addPrescriptionDetail(PrescriptionDetail prescriptionDetail) {
        Double zongSum=0.0;//实际要付的总金额
        Double yiBaoSum=0.0;//医保总共报销了多少金额
        Prescription prescription=new Prescription();
        prescription.setRegistryId(prescriptionDetail.getRegistryId());
        prescription.setPatientId(prescriptionDetail.getPatientId());
        for(Integer i=0;i<prescriptionDetail.getIds().size();i++){
            Drug drug = drugMapper.getDrugById(prescriptionDetail.getIds().get(i));
            if(prescriptionDetail.getIsMedicare()==2){//判断是否使用医保卡
                zongSum+=prescriptionDetail.getAmountList().get(i)*(drug.getPrice()*0.8);//实际价钱
                yiBaoSum+=prescriptionDetail.getAmountList().get(i)*drug.getPrice()-prescriptionDetail.getAmountList().get(i)*(drug.getPrice()*0.8);//医保报销金额
            }else{
                zongSum+=prescriptionDetail.getAmountList().get(i)*drug.getPrice();
            }
        }
        prescription.setPayFee(zongSum);
        prescription.setMedicareFee(yiBaoSum);
        prescription.setFee(zongSum+yiBaoSum);
        prescriptionMapper.addPrescription(prescription);
        for(Integer i=0;i<prescriptionDetail.getIds().size();i++){
            Drug drug = drugMapper.getDrugById(prescriptionDetail.getIds().get(i));
            if(prescriptionDetail.getIsMedicare()==2){//判断是否使用医保卡
                prescriptionDetail.setPayPrice(prescriptionDetail.getAmountList().get(i)*(drug.getPrice()*0.8));//实际价钱
                prescriptionDetail.setMedicarePrice(prescriptionDetail.getAmountList().get(i)*drug.getPrice()-
                        prescriptionDetail.getPayPrice());//医保报销金额
            }else{
                prescriptionDetail.setPayPrice(prescriptionDetail.getAmountList().get(i)*drug.getPrice());
                prescriptionDetail.setMedicarePrice(0.0);
            }
            prescriptionDetail.setDrugId(drug.getId());
            prescriptionDetail.setAmount(prescriptionDetail.getAmountList().get(i));
            prescriptionDetail.setPrescriptionId(prescription.getId());
            prescriptionDetail.setPrice(drug.getPrice());
            prescriptionDetailMapper.addPrescriptionDetail(prescriptionDetail);
        }
       return prescription;
    }
    /**
     * 获取所有的药方(可根据挂号单号)
     * @return
     */
    @Override
    public PageInfo<Prescription> getPrescriptionAll(Integer registryId,Integer curPage,Integer pageSize) {
        PageHelper.startPage(curPage,pageSize);
        List<Prescription> prescriptionList = prescriptionMapper.getPrescriptionAll(registryId);
        PageInfo<Prescription> pageInfo = new PageInfo<>(prescriptionList);
        return pageInfo;
    }

    /**
     * 根据药方id修改是否取药状态
     * @param prescriptionId
     * @return
     */
    @Override
    public Integer updateIsFetchMedicine(Integer prescriptionId) {
        return prescriptionMapper.updateIsFetchMedicine(prescriptionId);
    }

    /**
     * 修改药方支付状态和时间
     * @param prescriptionId
     * @return
     */
    @Override
    public Integer updatePayStatus(Integer prescriptionId) {
        return prescriptionMapper.updatePayStatus(prescriptionId);
    }

    /**
     * 根据id查询药方实际支付费用,查询对象
     * @param prescriptionId
     * @return
     */
    @Override
    public Prescription getByPrescriptionId(Integer prescriptionId) {
        return prescriptionMapper.getPrescriptionById(prescriptionId);
    }

    /**
     * 查询所有未支付的药方
     * @return
     */
    @Override
    public List<Prescription> getAllPrescriptionPay() {
        return prescriptionMapper.getAllPrescriptionPay();
    }

}
